package com.ailatrieuphu.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ailatrieuphu.R;
import com.ailatrieuphu.item.Room;

public class RoomAdapter extends ArrayAdapter<Room> {
	ArrayList<Room> items;
	Context context;
	public final int idStatus[] = { R.drawable.tt0, R.drawable.tt1,
			R.drawable.tt2, R.drawable.tt3, R.drawable.tt4, R.drawable.tt5 };

	public RoomAdapter(Context context, ArrayList<Room> items) {
		super(context, R.layout.row_room, items);
		this.items = items;
		this.context = context;
	}

	ViewHolder holder;

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		if (convertView == null) {
			convertView = View.inflate(context, R.layout.row_room, null);
			holder = new ViewHolder();
			holder.imStatus[0] = (ImageView) convertView
					.findViewById(R.id.rr_tt0);
			holder.imStatus[1] = (ImageView) convertView
					.findViewById(R.id.rr_tt1);
			holder.imStatus[2] = (ImageView) convertView
					.findViewById(R.id.rr_tt2);
			holder.imStatus[3] = (ImageView) convertView
					.findViewById(R.id.rr_tt3);
			holder.imStatus[4] = (ImageView) convertView
					.findViewById(R.id.rr_tt4);
			holder.layout = (LinearLayout) convertView
					.findViewById(R.id.rr_background);
			holder.name = (TextView) convertView.findViewById(R.id.rr_tvName);
			holder.minBet = (TextView) convertView
					.findViewById(R.id.rr_tvMinBet);
			convertView.setTag(holder);
		}
		holder = (ViewHolder) convertView.getTag();
		Room room = items.get(position);
		holder.name.setText(room.getName());
		holder.minBet.setText("Tien cuoc toi thieu: " + room.getMinBet());
		if (position % 2 == 0) {
			holder.layout.setBackgroundResource(R.drawable.dong1);
		} else {
			holder.layout.setBackgroundResource(R.drawable.dong2);
		}
		int status = room.getStatus();
		Log.e("status", status+" ");
		for (int i = 0; i < status; i++) {
			holder.imStatus[i].setImageResource(idStatus[i+1]);
		}

		return convertView;
	}

	class ViewHolder {
		public TextView name, minBet;
		public ImageView[] imStatus = new ImageView[5];
		public LinearLayout layout;
	}

}
